package core;

import branch.Branch;
import gitobject.Commit;

public class JitCheckout {
    /**
     * switch to a branch
     */
    public static void checkout(String branchName) throws Exception{
        if (!Branch.branchExist(branchName)){
            System.out.println("Branch " + branchName + " doesn't exist");
            return;
        }
        Branch.updateHEAD(branchName);
        Commit newCommit = Commit.deserialize(Branch.getCurrentCommitKey());
        JitReset.resetHard(newCommit);
        System.out.println("Switched to branch " + branchName);
    }

    /**
     * build and switch to a new branch
     */
    public static void checkoutBranch(String branchName) throws Exception{
        JitBranch.branch(branchName);
        checkout(branchName);
    }
}
